<template>
  <e-panel v-bind="opts">
    <e-table ref="tableRef" v-bind="opts.commProps.tableProps" :actions="rowActions" />
  </e-panel>
</template>

<script lang="ts" setup>
  import Model from './model'
  import { usePanel } from '@/components/ext'
  import { FormTypeEnum } from 'easy-descriptor'
  import type { ButtonActions } from '@/components/ActionButton/commonActions'

  defineOptions({
    name: 'SysDictList'
  })

  const opts = usePanel(Model, { formProps: { cols: 2 } })

  const { tableRef } = opts.refs

  const handleAddChild = ({ row }) => {
    opts.refs.formRef.value?.open(FormTypeEnum.ADD, {
      parentId: row.id,
      sortOrder: (row.children?.length ?? 0) + 1
    })
  }

  const rowActions = (row: Model): ButtonActions => {
    return row.parentId === '0'
      ? [
          {
            action: 'addChild',
            html: '添加',
            text: true,
            type: 'primary',
            perms: 'insert',
            handle: handleAddChild
          },
          'edit',
          'delete'
        ]
      : ['edit', 'delete']
  }
</script>

<style scoped lang="less"></style>
